package org.rcy.framework.api.entity;

import java.io.Serializable;
import java.util.LinkedHashMap;

/**
 * 封装返回结果
 */
public class ResultMessage<T> implements Serializable{
    private boolean flag;//执行结果，true为执行成功 false为执行失败
    private String message;//返回结果信息
    private LinkedHashMap<String,T> data = new LinkedHashMap<String,T>();//返回数据
    private Integer code;//自定义的状态码

    public ResultMessage() {
    }

    public ResultMessage(boolean flag, Integer code , String message) {
        super();
        this.flag = flag;
        this.code=code;
        this.message = message;
    }
    public ResultMessage(boolean flag, Integer code) {
        super();
        this.flag = flag;
        this.code=code;
    }
    public ResultMessage(boolean flag, Integer code, String message, T t) {
        this.flag = flag;
        this.code=code;
        this.message = message;
        this.data.put("data",t);
    }

    public boolean isFlag() {
        return flag;
    }
    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data.get("data");
    }

    public void setData(T t) {
        this.data.put("data",t);
    }

    public static <T> ResultMessage<T> success(){
        return new ResultMessage(true,200,"SUCCESS");
    }

    public static <T> ResultMessage<T> success(T data){
        return new ResultMessage(true,200,"SUCCESS",data);
    }

    public static <T> ResultMessage<T> error(){
        return new ResultMessage(false,100,"ERROR");
    }

    public static <T> ResultMessage<T> error(String message){
        return new ResultMessage(false,100,message);
    }

    public static <T> ResultMessage<T> error(StatusCode statusCode){
        return new ResultMessage(false, statusCode.getValue(), statusCode.getReasonPhrase());
    }

}

